package com.kreedx.kdemo.module;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.alibaba.fastjson.JSON;
import com.kreedx.kdemo.R;
import com.kreedx.kdemo.bean.ChatRequest;
import com.kreedx.kdemo.bean.LoginRequest;
import com.kreedx.kdemo.module.contract.SocketContract;
import com.kreedx.kdemo.module.presenter.SocketPresenter;
import com.qmuiteam.qmui.alpha.QMUIAlphaTextView;
import com.qmuiteam.qmui.widget.QMUIAnimationListView;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.xujiaji.xmvp.view.base.v4.XBaseFragment;

public class SocketFragment extends XBaseFragment<SocketPresenter> implements SocketContract.View {
    @BindView(R.id.top_bar)
    QMUITopBar topBar;
    @BindView(R.id.text_view)
    QMUIAlphaTextView textView;
    @BindView(R.id.list_view)
    QMUIAnimationListView listView;
    Unbinder unbinder;

    public static SocketFragment newInstance() {

        Bundle args = new Bundle();

        SocketFragment fragment = new SocketFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_socket;
    }

    @Override
    public void onInitCircle() {

        unbinder = ButterKnife.bind(this, getRootView());

        topBar.setTitle("Sokect");
        topBar.setSubTitle("未连接");

        topBar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Objects.requireNonNull(getActivity()).finish();
            }
        });

        String commands[]  = new String[]{
                "连接","断开","发送登录","发送消息"
        };
        listView.setAdapter(new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,commands));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        presenter.connect();
                        break;
                    case 1:
                        presenter.disconnect();
                        break;
                    case 2:
                        LoginRequest request = new LoginRequest();
                        request.setUsername("kree");
                        request.setPassword("123456");
                        presenter.sendMessage(1001,request);
                        break;
                    case 3:
                        showChatDialog();
                        break;
                }
            }
        });
        presenter.init();

    }

    private void showChatDialog() {
        final QMUIDialog.EditTextDialogBuilder editTextDialogBuilder = new QMUIDialog.EditTextDialogBuilder(getActivity());
        editTextDialogBuilder.setPlaceholder("请输入文字");
        editTextDialogBuilder.addAction(new QMUIDialogAction(getContext(), "确定", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                dialog.dismiss();

                String msg = editTextDialogBuilder.getEditText().getText().toString();
                if (!msg.equals(""))
                    presenter.sendMessage(1002, msg);

            }
        }));
        editTextDialogBuilder.show();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        presenter.disconnect();
        unbinder.unbind();
    }

    @Override
    public void upateState(SocketContract.Model.State state) {
        switch (state){
            case Connected:
                topBar.setSubTitle("已连接");
                break;
            case Connecting:
                topBar.setSubTitle("连接中");
                break;
            case Disconnected:
                topBar.setSubTitle("未连接");
                break;
        }
    }

    @Override
    public void onMessage(int code, String msg) {
        switch (code){
            case 1001:
                textView.setText(String.valueOf(msg));
                break;
            case 1002:
                ChatRequest chatRequest = JSON.parseObject(msg,ChatRequest.class);
                String text = String.format("%s说:<font color='red'>%s</font>",chatRequest.getUsername(),chatRequest.getMsg());
                textView.setText(Html.fromHtml(text));
                break;
        }
    }
}
